R Markdown

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.3     ✓ purrr   0.3.4
## ✓ tibble  3.0.5     ✓ dplyr   1.0.3
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(gt)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(patchwork)
sleep<-read.csv("SleepInMammals.csv")
sleep
##            Species.of.animal Body.Weight..kg. Brain.Weight..g.
## 1  African giant pouched rat            1.000             6.60
## 2             Asian elephant         2547.000          4603.00
## 3                     Baboon           10.550           179.50
## 4              Big brown bat            0.023             0.30
## 5            Brazilian tapir          160.000           169.00
## 6                        Cat            3.300            25.60
## 7                 Chimpanzee           52.160           440.00
## 8                 Chinchilla            0.425             6.40
## 9                        Cow          465.000           423.00
## 10     Eastern American mole            0.075             1.20
## 11                   Echidna            3.000            25.00
## 12         European hedgehog            0.785             3.50
## 13                    Galago            0.200             5.00
## 14                      Goat           27.660           115.00
## 15            Golden hamster            0.120             1.00
## 16                 Gray seal           85.000           325.00
## 17           Ground squirrel            0.101             4.00
## 18                Guinea pig            1.040             5.50
## 19                     Horse          521.000           655.00
## 20 Lesser short-tailed shrew            0.005             0.14
## 21          Little brown bat            0.010             0.25
## 22                     Human           62.000          1320.00
## 23                     Mouse            0.023             0.40
## 24                Musk shrew            0.048             0.33
## 25       N. American opossum            1.700             6.30
## 26     Nine-banded armadillo            3.500            10.80
## 27                Owl monkey            0.480            15.50
## 28              Patas monkey           10.000           115.00
## 29                Phanlanger            1.620            11.40
## 30                       Pig          192.000           180.00
## 31                    Rabbit            2.500            12.10
## 32                       Rat            0.280             1.90
## 33                   Red fox            4.235            50.40
## 34             Rhesus monkey            6.800           179.00
## 35                     Sheep           55.500           175.00
## 36                    Tenrec            0.900             2.60
## 37                Tree hyrax            2.000            12.30
## 38                Tree shrew            0.104             2.50
## 39                    Vervet            4.190            58.00
## 40             Water opossum            3.500             3.90
##    Slow.wave.sleep..hrs.day. Paradoxical.sleep..hrs.day. Total.sleep..hrs.day.
## 1                        6.3                         2.0                   8.3
## 2                        2.1                         1.8                   3.9
## 3                        9.1                         0.7                   9.8
## 4                       15.8                         3.9                  19.7
## 5                        5.2                         1.0                   6.2
## 6                       10.9                         3.6                  14.5
## 7                        8.3                         1.4                   9.7
## 8                       11.0                         1.5                  12.5
## 9                        3.2                         0.7                   3.9
## 10                       6.3                         2.1                   8.4
## 11                       8.6                         0.0                   8.6
## 12                       6.6                         4.1                  10.7
## 13                       9.5                         1.2                  10.7
## 14                       3.3                         0.5                   3.8
## 15                      11.0                         3.4                  14.4
## 16                       4.7                         1.5                   6.2
## 17                      10.4                         3.4                  13.8
## 18                       7.4                         0.8                   8.2
## 19                       2.1                         0.8                   2.9
## 20                       7.7                         1.4                   9.1
## 21                      17.9                         2.0                  19.9
## 22                       6.1                         1.9                   8.0
## 23                      11.9                         1.3                  13.2
## 24                      10.8                         2.0                  12.8
## 25                      13.8                         5.6                  19.4
## 26                      14.3                         3.1                  17.4
## 27                      15.2                         1.8                  17.0
## 28                      10.0                         0.9                  10.9
## 29                      11.9                         1.8                  13.7
## 30                       6.5                         1.9                   8.4
## 31                       7.5                         0.9                   8.4
## 32                      10.6                         2.6                  13.2
## 33                       7.4                         2.4                   9.8
## 34                       8.4                         1.2                   9.6
## 35                       3.2                         0.6                   3.8
## 36                      11.0                         2.3                  13.3
## 37                       4.9                         0.5                   5.4
## 38                      13.2                         2.6                  15.8
## 39                       9.7                         0.6                  10.3
## 40                      12.8                         6.6                  19.4
##    Maximum.life.span..years. Gestation.time..days. Predation.index
## 1                        4.5                  42.0               3
## 2                       69.0                 624.0               3
## 3                       27.0                 180.0               4
## 4                       19.0                  35.0               1
## 5                       30.4                 392.0               4
## 6                       28.0                  63.0               1
## 7                       50.0                 230.0               1
## 8                        7.0                 112.0               5
## 9                       30.0                 281.0               5
## 10                       3.5                  42.0               1
## 11                      50.0                  28.0               2
## 12                       6.0                  42.0               2
## 13                      10.4                 120.0               2
## 14                      20.0                 148.0               5
## 15                       3.9                  16.0               3
## 16                      41.0                 310.0               1
## 17                       9.0                  28.0               5
## 18                       7.6                  68.0               5
## 19                      46.0                 336.0               5
## 20                       2.6                  21.5               5
## 21                      24.0                  50.0               1
## 22                     100.0                 267.0               1
## 23                       3.2                  19.0               4
## 24                       2.0                  30.0               4
## 25                       5.0                  12.0               2
## 26                       6.5                 120.0               2
## 27                      12.0                 140.0               2
## 28                      20.2                 170.0               4
## 29                      13.0                  17.0               2
## 30                      27.0                 115.0               4
## 31                      18.0                  31.0               5
## 32                       4.7                  21.0               3
## 33                       9.8                  52.0               1
## 34                      29.0                 164.0               2
## 35                      20.0                 151.0               5
## 36                       4.5                  60.0               2
## 37                       7.5                 200.0               3
## 38                       2.3                  46.0               3
## 39                      24.0                 210.0               4
## 40                       3.0                  14.0               2
##    Sleep.exposure.index Overall.danger.index
## 1                     1                    3
## 2                     5                    4
## 3                     4                    4
## 4                     1                    1
## 5                     5                    4
## 6                     2                    1
## 7                     1                    1
## 8                     4                    4
## 9                     5                    5
## 10                    1                    1
## 11                    2                    2
## 12                    2                    2
## 13                    2                    2
## 14                    5                    5
## 15                    1                    2
## 16                    3                    1
## 17                    1                    3
## 18                    3                    4
## 19                    5                    5
## 20                    2                    4
## 21                    1                    1
## 22                    1                    1
## 23                    1                    3
## 24                    1                    3
## 25                    1                    1
## 26                    1                    1
## 27                    2                    2
## 28                    4                    4
## 29                    1                    2
## 30                    4                    4
## 31                    5                    5
## 32                    1                    3
## 33                    1                    1
## 34                    3                    2
## 35                    5                    5
## 36                    1                    2
## 37                    1                    3
## 38                    2                    2
## 39                    3                    4
## 40                    1                    1
sleep %>% 
  select(!Gestation.time..days.:Overall.danger.index) %>% 
  arrange(desc(Body.Weight..kg.)) %>% 
gt(rowname_col = "row",
   groupname_col = "group") %>% 
  tab_header(
    title = md("**Sleep Patterns in Mammals**")) %>% 
  tab_footnote(
    footnote = "Weight in kilograms", 
    locations = cells_column_labels(
      columns = 2)) %>% 
  tab_footnote(
    footnote = "Weight in grams",
    locations = cells_column_labels(
      columns = 3)) %>%
  tab_footnote(
    footnote = "Non-dreaming hours per day",
    locations = cells_column_labels(
      columns = 4)) %>%
  tab_footnote(
    footnote = "Dreaming hours per day",
    locations = cells_column_labels(
      columns = 5)) %>% 
  cols_label(
    Species.of.animal="Species",
    Body.Weight..kg.="Body Weight",
    Brain.Weight..g. ="Brain Weight",
    Slow.wave.sleep..hrs.day. = "Slow Wave Sleep",
    Paradoxical.sleep..hrs.day. = "Paradoxical Sleep",
    Total.sleep..hrs.day.   = "Total Sleep",
    Maximum.life.span..years. ="Maximum Life Span (yrs)")
Sleep Patterns in Mammals
Species Body Weight1 Brain Weight2 Slow Wave Sleep3 Paradoxical Sleep4 Total Sleep Maximum Life Span (yrs)
Asian elephant 2547.000 4603.00 2.1 1.8 3.9 69.0
Horse 521.000 655.00 2.1 0.8 2.9 46.0
Cow 465.000 423.00 3.2 0.7 3.9 30.0
Pig 192.000 180.00 6.5 1.9 8.4 27.0
Brazilian tapir 160.000 169.00 5.2 1.0 6.2 30.4
Gray seal 85.000 325.00 4.7 1.5 6.2 41.0
Human 62.000 1320.00 6.1 1.9 8.0 100.0
Sheep 55.500 175.00 3.2 0.6 3.8 20.0
Chimpanzee 52.160 440.00 8.3 1.4 9.7 50.0
Goat 27.660 115.00 3.3 0.5 3.8 20.0
Baboon 10.550 179.50 9.1 0.7 9.8 27.0
Patas monkey 10.000 115.00 10.0 0.9 10.9 20.2
Rhesus monkey 6.800 179.00 8.4 1.2 9.6 29.0
Red fox 4.235 50.40 7.4 2.4 9.8 9.8
Vervet 4.190 58.00 9.7 0.6 10.3 24.0
Nine-banded armadillo 3.500 10.80 14.3 3.1 17.4 6.5
Water opossum 3.500 3.90 12.8 6.6 19.4 3.0
Cat 3.300 25.60 10.9 3.6 14.5 28.0
Echidna 3.000 25.00 8.6 0.0 8.6 50.0
Rabbit 2.500 12.10 7.5 0.9 8.4 18.0
Tree hyrax 2.000 12.30 4.9 0.5 5.4 7.5
N. American opossum 1.700 6.30 13.8 5.6 19.4 5.0
Phanlanger 1.620 11.40 11.9 1.8 13.7 13.0
Guinea pig 1.040 5.50 7.4 0.8 8.2 7.6
African giant pouched rat 1.000 6.60 6.3 2.0 8.3 4.5
Tenrec 0.900 2.60 11.0 2.3 13.3 4.5
European hedgehog 0.785 3.50 6.6 4.1 10.7 6.0
Owl monkey 0.480 15.50 15.2 1.8 17.0 12.0
Chinchilla 0.425 6.40 11.0 1.5 12.5 7.0
Rat 0.280 1.90 10.6 2.6 13.2 4.7
Galago 0.200 5.00 9.5 1.2 10.7 10.4
Golden hamster 0.120 1.00 11.0 3.4 14.4 3.9
Tree shrew 0.104 2.50 13.2 2.6 15.8 2.3
Ground squirrel 0.101 4.00 10.4 3.4 13.8 9.0
Eastern American mole 0.075 1.20 6.3 2.1 8.4 3.5
Musk shrew 0.048 0.33 10.8 2.0 12.8 2.0
Big brown bat 0.023 0.30 15.8 3.9 19.7 19.0
Mouse 0.023 0.40 11.9 1.3 13.2 3.2
Little brown bat 0.010 0.25 17.9 2.0 19.9 24.0
Lesser short-tailed shrew 0.005 0.14 7.7 1.4 9.1 2.6

1 Weight in kilograms

2 Weight in grams

3 Non-dreaming hours per day

4 Dreaming hours per day

sleep_graph<-
  sleep %>% 
  mutate(body_wtlbs = Body.Weight..kg.*2.20462) %>%  
  arrange(desc(body_wtlbs)) %>% 
  head(25) %>% 
  ggplot(aes(
    y = body_wtlbs, 
    x = Total.sleep..hrs.day.,
    size = body_wtlbs,
    alpha = .5,
    color= Species.of.animal))+
  geom_point() +
  labs(title = "Total hours per day mammals of different weight (lbs) sleep",
       x = "",
       y = "") +
  theme(plot.title.position = "plot",
        legend.position = "None") 

ggplotly(sleep_graph) %>% 
  hide_legend()
sleep %>% 
    ggplot(aes(x = Overall.danger.index,
               y = fct_reorder(Species.of.animal,
                               Overall.danger.index,
                               .desc = FALSE),
               fill = Predation.index)) +
    geom_col() +
    labs(title = "Overall danger index of attacks from other animals for mammals",
         x = "",
         y = "") +
    theme(legend.position = "None",
          plot.title.position = "plot")

G0 <-
sleep %>% 
  mutate(percent_dreaming = (Paradoxical.sleep..hrs.day./Total.sleep..hrs.day.) * 100) %>% 
  filter(percent_dreaming != 0) %>% 
  ggplot() +
  geom_col(aes(x = percent_dreaming, 
               y = fct_reorder(Species.of.animal, percent_dreaming, median),
               fill = Total.sleep..hrs.day.)) +
  labs(title = "How Much do Mammals Dream",
       y = "",
       x = "Percentage of Hours Dreaming",
       fill = "Total Sleeping Hours") +
  scale_x_continuous(expand = c(0,0)) +
  scale_fill_gradient(high = "green", low = "darkgreen")
G0

G1 <-
  sleep %>% 
  filter(Species.of.animal %in% c("Horse", "Cow", "Pig", "Human", "Sheep", "Chimpanzee", "Goat", "Baboon", "Red fox", "Cat", "Rabbit", "Rat", "Ground squirrel", "Mouse")) %>% 
  select(Species.of.animal, Slow.wave.sleep..hrs.day.,Sleep.exposure.index) %>% 
  ggplot() +
  geom_col(aes(y = Slow.wave.sleep..hrs.day., 
               x = fct_reorder(Species.of.animal,Slow.wave.sleep..hrs.day.,median),
               fill = Sleep.exposure.index)) +
  geom_text(aes(y = Slow.wave.sleep..hrs.day. + 5, x=fct_reorder(Species.of.animal,Slow.wave.sleep..hrs.day.,median), label= Species.of.animal), color="white", fontface="bold",alpha=0.6, size= 2.5, inherit.aes = FALSE ) +
  scale_color_viridis_c(option = "magma") +
  labs(title = "Non-Dreaming Hours of Sleep per Day",
       caption = "Pippa Gallagher and Joselyn Angeles",
       subtitle = "This measures the number of hours of slow wave (non-dreaming) sleep each mammal, on avergae, has each night. The Sleep Measure Index determines how vulnerabel the mammal is when sleeping, with 1 being the least vulnerbale and 5 the most.",
       y = "",
       x = "",
       fill = "Sleep Exposure Index") +
  coord_polar(start = 0) +
  ylim(-3,18) +
   theme(
    axis.text.x = element_blank(),
    panel.grid = element_blank(),
    plot.background = element_rect(fill = "#0a203b"),
    panel.background = element_rect(fill = "#0a203b"),
    legend.background = element_rect(fill = "#0a203b"),
    plot.title = element_text(color = "white"),
    legend.text = element_text(color = "white"),
    legend.title = element_text(color = "white"),
    axis.text.y = element_text(color = "white"),
    plot.caption = element_text(color = "white"),
    plot.subtitle = element_text(color = "white")) +
  scale_fill_gradient(high = "#2473db", low = "#c3deff")
  
G1

G2 <-
  sleep %>% 
  filter(Species.of.animal %in% c("Horse", "Cow", "Pig", "Human", "Sheep", "Chimpanzee", "Goat", "Baboon", "Red fox", "Cat", "Rabbit", "Rat", "Ground squirrel", "Mouse")) %>% 
  select(Species.of.animal,Paradoxical.sleep..hrs.day. ,Sleep.exposure.index) %>% 
  ggplot() +
  geom_col(aes(y = Paradoxical.sleep..hrs.day., 
               x = fct_reorder(Species.of.animal,Paradoxical.sleep..hrs.day.,median),
               fill = Sleep.exposure.index)) +
  geom_text(aes(y = Paradoxical.sleep..hrs.day. + 2, x=fct_reorder(Species.of.animal,Paradoxical.sleep..hrs.day.,median), label= Species.of.animal), color="white", fontface="bold",alpha=0.6, size=2.5, inherit.aes = FALSE ) +
  scale_color_viridis_c(option = "magma") +
  labs(title = "Dreaming Hours of Sleep per Day",
       caption = "Pippa Gallagher and Joselyn Angeles",
       subtitle = "This measures the number of hours of paradoxical (dreaming) sleep each mammal, on avergae, has each night. The Sleep Measure Index determines how vulnerabel the mammal is when sleeping, with 1 being the least vulnerbale and 5 the most.",
       y = "",
       x = "",
       fill = "Sleep Exposure Index") +
  coord_polar(start = 0) +
  ylim(-1.5,6) +
     theme(
    axis.text.x = element_blank(),
    panel.grid = element_blank(),
    plot.background = element_rect(fill = "#0a203b"),
    panel.background = element_rect(fill = "#0a203b"),
    legend.background = element_rect(fill = "#0a203b"),
    plot.title = element_text(color = "white"),
    legend.text = element_text(color = "white"),
    legend.title = element_text(color = "white"),
    axis.text.y = element_text(color = "white"),
    plot.caption = element_text(color = "white"),
    plot.subtitle = element_text(color = "white")) +
  scale_fill_gradient(high = "#2473db", low = "#c3deff")
G2

sleep %>% 
  ggplot() +
  geom_violin(aes(x = Overall.danger.index,
                  y = Maximum.life.span..years.,
                  color = Overall.danger.index)) +
  theme(
    plot.background = element_rect("black"),
    panel.background = element_rect("black"),
    plot.title = element_text(color = "lightgrey"),
    panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank(),
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    legend.position = "none"
  ) +
  labs(x = "",
       y = "",
       title = "Maximum Life Span and Danger Index")